home *** CD-ROM | disk | FTP | other *** search
- Path: news.sinet.slb.com!usenet
- From: "Vinh D. Nguyen" <vnguyen@sugar-land.anadrill.slb.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Help - problem with fstream and random-access files
- Date: Fri, 15 Mar 1996 08:54:37 -0600
- Organization: Schlumberger Anadrill
- Message-ID: <314984AD.388D@sugar-land.anadrill.slb.com>
- References: <4i4dnr$ld2@newsbf02.news.aol.com>
- NNTP-Posting-Host: 163.185.118.40
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- HAfshar wrote:
- >
- > Hello,
- >
- > I'm trying to create a random-access file of fixed-length records. Each
- > record (struct) has a couple of integers and a couple of arrays of
- > characters. I initialize the file with zero for each field, since the
- > number of records in the file is fixed and I would like to return zero for
- > access to a record that has not yet been written.
- >
- > I start out writing records to the file and everything seems okay but when
- > I try to read back the records and print them to the screen or to a file,
- > the first 9 read back unexpected values (but formatted correctly). After
- > that, the 10th record seems to be corrupted, and then the file seems to
- > get back in synch again.
- >
- > I stepped the program through a debugger and noticed that the get and put
- > file pointers seem to be at an unexpected location (for example I expect
- > the next record to be at address 0x400 but "tellg" tells me that it is
- > actually at 0x402.
-
- Does this record corruption and resynching seem to be cyclical, i.e., happening
- every 10 records or so? If it does then I suspect that you may have a structure
- alignment problem. The best way to get rid of this problem is to read/write
- each record in one fell swoop as follows:
-
- read( file, &mystruct, sizeof( mystruct ) );
- write( file, &mystruct, sizeof( mystruct ) );
-
- That is my best guest given the info you posted. If this doesn't fix your problem,
- you will need to provide more info, maybe your structure declaration and some sample code...
-
- --
- --------------------------------------------------------------------------
- * Vinh Nguyen vnguyen@slb.com *
- * Drilling Information Products - Senior Engineer *
- * Anadrill Schlumberger *
- * 200 Gillingham Ln. (713) 275-7524 (Office) *
- * Sugarland, TX 77478 (713) 275-8098 (FAX) *
- --------------------------------------------------------------------------
-